package com.ailk.kbi.kbirazor.component
{
	
	import flash.events.Event;
	
	import mx.controls.Alert;
	import mx.controls.Spacer;
	import mx.core.IVisualElement;
	
	import spark.components.HGroup;
	import spark.components.VGroup;
	
	
	/**
	 * 
	 * 节点组，包含一个节点，锚点和子节点组
	 */
	public class NodeGroup extends HGroup{
		
		private var _contentXML:XML;
		private var _mainNode:Node;
		private var _anchor:Anchor;
		private var _childrenGroup:ChildrenGroup;
		
		public function NodeGroup(contentXML:XML){
			super();
//			this.setStyle();
			_contentXML = contentXML;
			updateChildren();
		}
		
		private function updateChildren():void{
			try{
				this.gap = 0;
				this.verticalAlign = "middle";
				
				/**********添加部分start*********/
				var warnFlag:Boolean = false;
				for each(var kpiXML:XML in _contentXML..kpi){
					if(kpiXML.hasOwnProperty('@warn_flag')){
						warnFlag = true;
					}
				}
				if(!warnFlag && !_contentXML.hasOwnProperty('@close')) {
					_contentXML.@close = true;
				}
				
				/**********添加部分end*********/
				
				_mainNode = new Node(_contentXML);			
				this.addElement(_mainNode);
				
				_mainNode.addEventListener("widthChanged", widthChangedHandler);
				_mainNode.addEventListener("heightChanged", heightChangedHandler);
				
				if(_contentXML.node.length() > 0){
					_anchor = new Anchor(_contentXML);
					this.addElement(_anchor);
					
					
					/**********添加部分start*********/
					var warnFlag1:Boolean = false;
					for each(var kpiXML1:XML in _contentXML..kpi){
						if(kpiXML1.hasOwnProperty('@warn_flag')){
							warnFlag1 = true;
						}
					}
					if(!warnFlag1 && !_contentXML.hasOwnProperty('@close')) {
						_anchor.close = true;
					}
					if(_contentXML.hasOwnProperty('@level') && _contentXML.@level == '1') {
						_anchor.close = false;
					}
					/**********添加部分end*********/
					
					if(!_anchor.close){					
						_childrenGroup = new ChildrenGroup(_contentXML);
						this.addElement(_childrenGroup);
						_childrenGroup.addEventListener("widthChanged", widthChangedHandler);
						_childrenGroup.addEventListener("heightChanged", heightChangedHandler);
					}
				}	
			}
			catch(error:Error){
				Alert.show(error.message);
			}
		}
		
		private function widthChangedHandler(event:Event):void {
			var newWidth:Number = (this.numElements - 1) * this.gap;
			for(var index:int = 0; index < this.numElements; index++){
				var element:IVisualElement = this.getElementAt(index);
				newWidth = newWidth + element.width;
			}
			this.width = newWidth;
			
		}
		
		private function heightChangedHandler(event:Event):void {
			var newHeigth:Number = 0;
			for(var index:int = 0; index < this.numElements; index++){
				var element:IVisualElement = this.getElementAt(index);
				newHeigth = Math.max(newHeigth, element.height);
			}
			this.height = newHeigth;
		}
	}
}